<?php
include '../includes/cDatabase.php';
include '../includes/variables.php';
include '../includes/cPage.php';
include '../includes/cDates.php';

$cPage = new page("Update Config Database");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

$selectArray = array();
if (isset($_GET["config"]))
{
	$cDatabase = new database();

	$connect = $cDatabase->connectToDatabase();
	$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
	$db = $cDatabase->selectDatabase($connect, $activeDatabase);

	$selectArray[0] = '*';
	$result = $cDatabase->selectFromDatabase($connect, 'config', $selectArray, NULL, NULL, NULL, NULL);
	
	$finfo = mysqli_fetch_fields($result);
	
	for ($i = 0; $i < mysqli_num_fields($result); $i++)
	{
		if (isset($_POST[$finfo[$i]->name]))	// If it is this field which has been posted
		{
			$selectColumn = $finfo[$i]->name;
			$setValue = $_POST[$finfo[$i]->name];
		}
	}
	
	// Update this entry
	$query = "UPDATE config SET ".$selectColumn." = '".$setValue."' WHERE loginName='root'";
	
	$result = mysqli_query($connect, $query);
	
	if ($result == 1)
	{
		echo"<BR>Item has been updated.";
	}
	else
	{
		echo"<BR>ERROR: Item has not been updated.";
	}
}
else
{
	$cDatabase = new database();

	$connect = $cDatabase->connectToDatabase();
	$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
	$db = $cDatabase->selectDatabase($connect, $activeDatabase);

	$selectArray[0] = '*';
	$result = $cDatabase->selectFromDatabase($connect, 'config', $selectArray, NULL, NULL, NULL, NULL);

	if ($result)
	{
		$PHP_SELF = $_SERVER['PHP_SELF'];

		$finfo = mysqli_fetch_fields($result);
		$row = mysqli_fetch_array($result, MYSQLI_NUM);
		echo "<table>";
		for($i = 0; $i < mysqli_num_fields($result); $i++)
		{
			if ($finfo[$i]->name != 'loginName' && $finfo[$i]->name != 'password')
			{
				$buttonName = $finfo[$i]->name."Button";
				$idName = $finfo[$i]->name;
			
				echo "<FORM ACTION=\"$PHP_SELF?config=yes\" METHOD=\"POST\"><td>";
				echo "<tr>";
				echo "<tr><td>" .$finfo[$i]->name."</td>";
				echo "<td><input type='text' name='".$idName."' size='".$inputBoxSize;
				echo "' maxlength='4' value='".$row[$i]."'></td>";
				echo "<td><input name='".$buttonName."' type='submit' id='".$idName."' value='Update'></td></form></td>";
				echo "</tr>";
			}
		}
		echo "</tr>";
		echo "</table>";
	}
}
echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
?>
